package com.wind.taobao.core.webservice
{
	import com.wind.taobao.api.IPrivateAPI;
	import com.wind.taobao.api.IPublicAPI;
	import com.wind.taobao.core.config.TaobaoConfig;
	import com.wind.taobao.core.util.TaobaoConstants;
	import com.wind.taobao.core.util.TaobaoUtils;
	import com.wind.taobao.core.webservice.event.TaobaoRequestEvent;

	import flash.events.EventDispatcher;
	import flash.net.FileReference;

	import mx.collections.ArrayCollection;

	[Event(name="complete", type="com.wind.taobao.core.webservice.event.TaobaoRequestEvent")]
	/**
	 * 淘宝请求
	 * @author fxy
	 */
	public class TaobaoRequest extends EventDispatcher
	{
		/**
		 * app_key
		 */
		[Bindable]
		public var app_key:String;
		private var _method:String;
		/**
		 * timestamp
		 */
		[Bindable]
		public var timestamp:String;
		/**
		 * v
		 */
		[Bindable]
		public var v:String;
		/**
		 * format
		 */
		[Bindable]
		public var format:String="json";
		/**
		 * sign
		 */
		[Bindable]
		public var sign:String;
		/**
		 * session
		 */
		[Bindable]
		public var session:String;
		/**
		 * secret
		 */
		[Bindable]
		public var secret:String;
		/**
		 * 是否是沙箱请求
		 */
		[Bindable]
		public var isSandbox:Boolean=false;

		public function validate():void
		{
			//参数校验
		}

		public function getParameters(props:Array, validate:Boolean=true):ArrayCollection
		{
			if (!this.app_key)
			{
				this.app_key=TaobaoConfig.appKey;
				this.secret=TaobaoConfig.secret;
			}

			var date:Date=new Date();
			date.setTime(date.getTime() + TaobaoUtils.timeDifference);
			this.timestamp=TaobaoUtils.dateToStr(date);

			if (!this.v)
				this.v=TaobaoConfig.version;
			if (!this.format) //默认JSON
				this.format="json";
			if (!(this is IPublicAPI) && validate) //如果不是公共API
			{
				if (this is IPrivateAPI) //如果是私有API
				{
					if (!this.session && !TaobaoConfig.session)
					{
						throw new Error("用户尚未登录授权");
					}
				}
				if (!this.session && TaobaoConfig.session)
					this.session=TaobaoConfig.session;
			}
			var params:ArrayCollection=getProperties(props);
			var sign:Object=new Object();
			sign["key"]="sign";
			sign["value"]=TaobaoUtils.signature(params, this.secret, TaobaoConstants.SIGN);
			params.addItem(sign);
			dispatchEvent(new TaobaoRequestEvent(TaobaoRequestEvent.PARAMETERS_COMPLETE, params));
			return params;
		}

		private function getProperties(props:Array):ArrayCollection
		{
			var properties:ArrayCollection=new ArrayCollection();
			for each (var q:QName in props)
			{
				var prop:String=q.localName;
				if (prop != "sign" && prop != "secret" && prop != "isSandbox")
				{
					if (TaobaoUtils.hasValue(this[prop])) //过滤掉NULL值
					{
						if (!(this[prop] is FileReference)) //过滤掉File
						{
							var obj:Object=new Object();
							if (prop.indexOf("__") != -1) //如果请求参数包含__则替换为.并删除__属性
							{
								obj.value=this[prop];
								obj.key=prop.replace("__", ".");
							}
							else
							{
								obj.value=this[prop];
								obj.key=prop;
							}
							properties.addItem(obj);
						}
					}
				}
			}
			return properties;
		}

		public function get method():String
		{
			return _method;
		}

		public function set method(value:String):void
		{
			this._method=value;
		}

	}
}